package net.zeus.redis.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisPoolUtils {
	
	private static volatile JedisPool jedisPool;
	
	private JedisPoolUtils () {}
	
	public static JedisPool getJedisPoolInstance() {
		
		if(jedisPool == null) {
			synchronized (JedisPoolUtils.class) {
				JedisPoolConfig jpc = new JedisPoolConfig();
				jpc.setMaxIdle(32);
				jpc.setMaxTotal(1000);
				jpc.setMaxWaitMillis(100000);
				jpc.setTestOnBorrow(true);
				
				jedisPool = new JedisPool(jpc, "127.0.0.1", 6379);
			}
		}
		
		return jedisPool;
	}
	
	public static Jedis get() {
		return getJedisPoolInstance().getResource();
	}
	
	public static void release(Jedis jedis) {
		if(jedis == null) {
			getJedisPoolInstance().returnResource(jedis);
		}
	}
}
